package mirkofrancuski.battleship.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import mirkofrancuski.battleship.game.Coordinates;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CoordinateTable {
	private static String DEBUG_TAG = "DEBUG CoordinateTable";
	
	private static final String COORDINATE_TABLE = "Coordinate_Table";
	private static final String COORDINATE_CLASS = "Class";
	private static final String COORDINATE_NAME = "Name";
	private static final String COORDINATE_HORIZONTAL = "Horizontal";
	private static final String COORDINATE_VERTICAL = "Vertical";
	
	private static final String CREATE_COORDINATE_TABLE = 
			"CREATE TABLE "	+ COORDINATE_TABLE 
			+ " (" 
			+ COORDINATE_CLASS + " TEXT, "
			+ COORDINATE_NAME + " TEXT, " 
			+ COORDINATE_HORIZONTAL + " INTEGER, " 
			+ COORDINATE_VERTICAL + " INTEGER" 
			+ ")";
	
	public static void onCreate(SQLiteDatabase database) {
		database.execSQL(CREATE_COORDINATE_TABLE);
	}
	
	public static void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		Log.w(HighScoreTable.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS " + CREATE_COORDINATE_TABLE);
		onCreate(database);
	}
	
	public static void addCoordinates(SQLiteDatabase db, String className, HashMap<String,Coordinates> coordinatesHashMap){
		if(coordinatesHashMap.isEmpty())
			return;
		
		Iterator<String> it = coordinatesHashMap.keySet().iterator();
		while(it.hasNext()){
			String name = it.next();
			Coordinates coord = coordinatesHashMap.get(name);
			
			ContentValues cv = new ContentValues();
			cv.put(COORDINATE_CLASS, className);
			cv.put(COORDINATE_NAME, name);
			cv.put(COORDINATE_HORIZONTAL, coord.horizontal);
			cv.put(COORDINATE_VERTICAL, coord.vertical);
			
			db.insert(COORDINATE_TABLE, null, cv);
		}
		
	}
	
	
	public static HashMap<String,Coordinates> getCoordinates(SQLiteDatabase db, String className){
		HashMap<String,Coordinates> coordinates = new HashMap<String, Coordinates>();
		String selectQuery = "SELECT " + COORDINATE_NAME + ", " + COORDINATE_HORIZONTAL + ", " + COORDINATE_VERTICAL + 
				" FROM " + COORDINATE_TABLE + " WHERE " + COORDINATE_CLASS + " = " + className;
		Cursor cursor = null;
		
		try {
			cursor = db.rawQuery(selectQuery, null);
			
			for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
				String name = cursor.getString(0); //variable name
				int horizontal = Integer.parseInt(cursor.getString(1)); // horizntoal
				int vertical = Integer.parseInt(cursor.getString(2)); // vertical
				
				coordinates.put(name, new Coordinates(horizontal, vertical));
			}
		} finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }		
		}				
		return coordinates;
	}
	
	public static void deleteCoordinates(SQLiteDatabase db, String className){
		db.delete(COORDINATE_TABLE, COORDINATE_CLASS + " = ?", new String[] { className });			
	}
	
	
	

}
